package com.situ.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.pojo.bo.ProductSearchBO;
import com.situ.mall.pojo.entity.Product;
import com.situ.mall.service.IProductService;
import com.situ.mall.util.JSONResult;

@Service
public class ProductServiceImpl implements IProductService{
	@Autowired
	private ProductMapper productMapper;

	@Override
	public JSONResult selectByPage(Integer page, Integer limit, ProductSearchBO productSearchBO) {
		int offset = (page - 1) * limit;
		List<Product> list = productMapper.selectByPage(offset, limit, productSearchBO);
		
		int count = productMapper.selectTotalCount(productSearchBO);
		return JSONResult.success(list, count);
	}

	@Override
	public boolean deleteByPrimaryKey(Integer id) {
		int count = productMapper.deleteByPrimaryKey(id);
		return count == 1;
	}

	@Override
	public boolean deleteAll(Integer[] ids) {
		System.out.println(ids);
		int count = productMapper.deleteByArray(ids);
		return count == ids.length;
	}

	@Override
	public boolean insert(Product product) {
		int count = productMapper.insert(product);
		return count == 1;
	}

	@Override
	public List<Product> selectByCategoryId(Integer categoryId) {
		List<Product> list = productMapper.selectByCategoryId(categoryId);
		return list;
	}

	@Override
	public Product selectProductById(Integer productId) {
		Product product = productMapper.selectByPrimaryKey(productId);
		return product;
	}

	@Override
	public boolean update(Product product) {
		int count = productMapper.update(product);
		return count == 1;
	}
}
